const db = require("../db/index");
const moment = require('moment')

exports.patientlists = (req, res) => {
    var sql =
        "select * from patient";
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
 

// 新增数据
exports.patientlistsinsert = (req, res) => {
    const { name , sex , type , idcard , birthDate , age , nationality , folk , maritalstatus , career , phone , eMail , address , nowDetail , nowzipcode , hukouaddress , nowhukouDetail , hukoucode , relotion , people , guardianshipphone , relotion1 , people1 , guardianshipphone1 , relotion2 , people2 , guardianshipphone2 , Patientnature , Medicalinsurance} = req.body
    var sql =
        `INSERT INTO patient(Medicalrecord,Medicalcard,name,sex,IDtype,Idcard,status,birthDate,age,nationality,folk,maritalstatus,career,phone,eMail,address,nowDetail,nowzipcode,hukouaddress,nowhukouDetail,hukoucode,relotion,people,guardianshipphone,relotion1,people1,guardianshipphone1,relotion2,guardianshippeople2,phone2,Patientnature,Medicalinsurance) VALUES('6520050870','20050870','${name}','${sex}','${type}','${idcard}','正常','${birthDate?birthDate:''}',${age},'${nationality?nationality:''}','${folk?folk:''}','${maritalstatus?maritalstatus:''}','${career?career:''}','${phone}','${eMail?eMail:''}',' ${address.join(',')}','${nowDetail}','${nowzipcode?nowzipcode:''}', '${hukouaddress?hukouaddress.join(','):''}','${nowhukouDetail?nowhukouDetail:''}','${hukoucode?hukoucode:''}','${relotion}','${people}','${guardianshipphone}','${relotion1?relotion1:''}','${people1?people1:''}','${guardianshipphone1?guardianshipphone1:''}','${relotion2?relotion2:''}','${people2?people2:''}','${guardianshipphone2?guardianshipphone2:''}','${Patientnature}','${Medicalinsurance?Medicalinsurance:''}')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            data,
            status:200
        });
    });
};

exports.patientlistsinserts = (req, res) => {
    const { Medicalrecord , Medicalcard , name , sex , IDtype , Idcard , birthDate , age , nationality , folk , maritalstatus , career , phone , eMail , address , nowDetail , nowzipcode , hukouaddress , nowhukouDetail , hukoucode , relotion , people , guardianshipphone , relotion1 , people1 , guardianshipphone1 , relotion2 , people2 , guardianshipphone2 , Patientnature  , Medicalinsurance} = req.query
    var sql =
        `INSERT INTO patient(Medicalrecord,Medicalcard,name,sex,IDtype,Idcard,status,birthDate,age,nationality,folk,maritalstatus,career,phone,eMail,address,nowDetail,nowzipcode,hukouaddress,nowhukouDetail,hukoucode,relotion,people,guardianshipphone,relotion1,people1,guardianshipphone1,relotion2,guardianshippeople2,phone2,Patientnature,Medicalinsurance) VALUES('${Medicalrecord}','${Medicalcard}','${name}','${sex}','${IDtype}','${Idcard}','正常','${birthDate?birthDate:''}',${age},'${nationality?nationality:''}','${folk?folk:''}','${maritalstatus?maritalstatus:''}','${career?career:''}','${phone}','${eMail?eMail:''}',' ${address.join(',')}','${nowDetail}','${nowzipcode?nowzipcode:''}', '${hukouaddress?hukouaddress.join(','):''}','${nowhukouDetail?nowhukouDetail:''}','${hukoucode?hukoucode:''}','${relotion}','${people}','${guardianshipphone}','${relotion1?relotion1:''}','${people1?people1:''}','${guardianshipphone1?guardianshipphone1:''}','${relotion2?relotion2:''}','${people2?people2:''}','${guardianshipphone2?guardianshipphone2:''}','${Patientnature?Patientnature:'自费'}','${Medicalinsurance?Medicalinsurance:''}')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send({
            data,
            status:200
        });
    });
};


// 搜索
exports.patientserch = (req, res) => {
    const { Medicalcard , Idcard , name , Filingtime , status } = req.body
    var sql =
        `select * from patient where ${Medicalcard?`Medicalcard like '%${Medicalcard}%'`:''} ${Idcard?`and Idcard like '%${Idcard}%'`:''} ${name?`and name like '%${name}%'`:''} ${Filingtime?`and Filingtime like '%${Filingtime}%'`:""}  ${status?(status=='全部状态'?'':`and status like '%${status}%'`):''}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data);
        }
        
    });
};

// 根据id查询信息
exports.patientid = (req, res) => {
    var sql =
        `select * from patient where patient_id=${req.query.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};
// 根据id查询就诊卡
exports.patientcard = (req, res) => {
    var sql = 
        `select * from patientcard where patient_id=${req.query.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send(data)
        }
    })
}
// 编辑患者信息
exports.patientupdate = (req, res) => {
    const { name , sex , IDtype , Idcard , birthDate , age , nationality , folk , maritalstatus , career , phone , eMail , address , nowDetail , nowzipcode , hukouaddress , nowhukouDetail , hukoucode , relotion , people , guardianshipphone , relotion1 , people1 , guardianshipphone1 , relotion2 , phone2 , guardianshipphone2 , Patientnature , Medicalinsurance} = req.body
    var sql = 
        `update patient set name='${name}',sex='${sex}',IDtype='${IDtype}',Idcard='${Idcard}',birthDate='${birthDate?birthDate:''}',age=${age},nationality='${nationality?nationality:''}',folk='${folk?folk:''}',maritalstatus='${maritalstatus?maritalstatus:''}',career='${career?career:''}',phone='${phone}',eMail='${eMail?eMail:''}',address='${address.join(',')}',nowDetail='${nowDetail}',nowzipcode='${nowzipcode?nowzipcode:""}',hukouaddress='${hukouaddress?hukouaddress.join(','):''}',nowhukouDetail='${nowhukouDetail?nowhukouDetail:''}',relotion='${relotion}',people='${people}',guardianshipphone='${guardianshipphone}',relotion1='${relotion1?relotion1:''}',people1='${people1?people1:''}',guardianshipphone1='${guardianshipphone1?guardianshipphone1:''}',relotion2='${relotion2?relotion2:''}',phone2='${phone2}',guardianshippeople2='${guardianshipphone2?guardianshipphone2:''}',Patientnature='${Patientnature}',Medicalinsurance='${Medicalinsurance}' where patient_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            res.send({
                data,
                status:200
            })
        }
    })
}


// 注销操作
// exports.patientlogoff = (req, res) => {
//     console.log(req.body)
//     var sql = 
//         `update patient set status='已注销',reson='${req.body.reson}',note='${req.body.note}' where patient_id=${req.body.id}`;
//     db.query(sql, [], (err, data) => {
//         if (err) {
//             console.log("出错了", err);
//             return res.send("错误" + err.message);
//         }else{
//             var sql =
//                 "select * from patient";
//             db.query(sql, [], (err, data) => {
//                 if (err) {
//                     console.log("出错了", err);
//                     return res.send("错误" + err.message);
//                 }
//                 res.send(data);
//             });
//         }
//     })
// }

// 注销
exports.patientlogoff = (req, res) => {
    var sql = 
        `update patient set status='已注销',reson='${req.body.reson}',note='${req.body.note}' where patient_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                "select * from patient";
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
    })
}
// 就诊卡挂失作废操作
exports.patientcardlogoff = (req, res) => {
    var sql = 
        `update patientcard set status='已${req.body.status}',cardnote='该卡已${req.body.status}',failuretime='${moment(new Date()).format("YYYY-MM-DD HH:mm:ss")}' where patientCard_id=${req.body.id}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                "select * from patientcard";
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
    })
}
// 就诊卡新增操作
exports.patientcardinsert = (req, res) => {
    const { patient_id , card , cardtype } = req.body
    var sql = 
        `insert into patientcard(patient_id,card,status,correlationtime,cardtype) values('${patient_id}','${card?card:Date.now().toString().slice(-8).padStart(8, '0')}','绑定中','${moment(new Date()).format("YYYY-MM-DD HH:mm:ss")}','${cardtype}')`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }else{
            var sql =
                "select * from patientcard";
            db.query(sql, [], (err, data) => {
                if (err) {
                    console.log("出错了", err);
                    return res.send("错误" + err.message);
                }
                res.send(data);
            });
        }
    })
}



// 根据病历号查询患者信息
exports.patientmedical = (req, res) => {
    var sql =
        `select * from patient where Medicalrecord=${req.query.Medicalrecord}`;
    db.query(sql, [], (err, data) => {
        if (err) {
            console.log("出错了", err);
            return res.send("错误" + err.message);
        }
        res.send(data);
    });
};